package com.agriculture.controller;

import com.agriculture.common.R;
import com.agriculture.entity.Category;
import com.agriculture.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.agriculture.dto.GoodDto;
import com.agriculture.entity.Good;
import com.agriculture.service.GoodService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品管理
 */
@CrossOrigin
@RestController
@RequestMapping("/good")
@Slf4j
public class GoodController {
    @Autowired
    private GoodService goodService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增产品
     * @param good
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Good good){
        log.info(good.toString());
        // 设置当前农户登录的id
//        good.setSellerId(BaseContext.getCurrentId());
        //新增
        goodService.save(good);

        return R.success("新增商品成功");
    }

    /**
     * 商品信息分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){

        //构造分页构造器对象
        Page<Good> pageInfo = new Page<>(page,pageSize);
        Page<GoodDto> goodDtoPage = new Page<>();

        //条件构造器
        LambdaQueryWrapper<Good> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(name != null, Good::getName,name);
        //添加排序条件
        queryWrapper.orderByDesc(Good::getUpdateTime);
        queryWrapper.eq(Good::getIsDeleted,0);
        //执行分页查询
        goodService.page(pageInfo,queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo,goodDtoPage,"records");

        List<Good> records = pageInfo.getRecords();

        List<GoodDto> list = records.stream().map((item) -> {
            GoodDto goodDto = new GoodDto();

            BeanUtils.copyProperties(item,goodDto);

            Long categoryId = item.getCategoryId();//分类id
            //根据id查询分类对象
            Category category = categoryService.getById(categoryId);

            if(category != null){
                String categoryName = category.getName();
                goodDto.setCategoryName(categoryName);
            }
            return goodDto;
        }).collect(Collectors.toList());

        goodDtoPage.setRecords(list);

        return R.success(goodDtoPage);
    }

    /**
     * 根据id查询商品信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Good> get(@PathVariable Long id){

        Good good = goodService.getById(id);

        return R.success(good);
    }

    /**
     * 修改商品
     * @param goodDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody GoodDto goodDto){
        log.info(goodDto.toString());

        goodService.updateById(goodDto);

        return R.success("修改商品成功");
    }

    /**
     * 根据条件查询对应的商品数据
     * @param good
     * @return
     */
    @GetMapping("/list")
    public R<List<GoodDto>> list(Good good){
        //构造查询条件
        LambdaQueryWrapper<Good> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(good.getCategoryId() != null , Good::getCategoryId,good.getCategoryId());
        //添加条件，查询状态为1（起售状态）的菜品
        queryWrapper.eq(Good::getIsDeleted,0);

        //添加排序条件
//        queryWrapper.orderByAsc(Good::getSort).orderByDesc(Good::getUpdateTime);

        List<Good> list = goodService.list(queryWrapper);

        List<GoodDto> goodDtoList = list.stream().map((item) -> {
            GoodDto goodDto = new GoodDto();

            BeanUtils.copyProperties(item,goodDto);

            Long categoryId = item.getCategoryId();//分类id
            //根据id查询分类对象
            Category category = categoryService.getById(categoryId);

            if(category != null){
                String categoryName = category.getName();
                goodDto.setCategoryName(categoryName);
            }
            return goodDto;
        }).collect(Collectors.toList());

        return R.success(goodDtoList);
    }

}
